package cn.cnlee.app.repository;

import cn.cnlee.app.config.Constants;
import cn.cnlee.app.pojo.IdVo;
import cn.cnlee.app.pojo.NewsContentDTO;
import cn.cnlee.app.remote.response.ResponseTransformer;
import cn.cnlee.app.remote.rx.RxSubscriber;
import cn.cnlee.app.util.StringUtil;
import cn.cnlee.arch.rx.RxSchedulers;
import cn.cnlee.arch.util.Logger;
import cn.cnlee.arch.view.stateview.StateConstants;

public class NewsRepository extends BaseRepository {
    protected final static String TAG = NewsRepository.class.getSimpleName();
    public static String EVENT_KEY_NEW_CONTENT_DATA = null;

    public NewsRepository() {
        EVENT_KEY_NEW_CONTENT_DATA = StringUtil.sBlank(EVENT_KEY_NEW_CONTENT_DATA, StringUtil.createEventKey());
    }

    public void getArticleContent(IdVo idVo) {
        addDisposable(apiService.getNewsContent(idVo)
                .compose(ResponseTransformer.handleFlowableResult())
                .compose(RxSchedulers.io_main())
                .subscribeWith(new RxSubscriber<NewsContentDTO>() {

                    @Override
                    protected void showLoading() {
                        postState(StateConstants.LOADING_STATE);
                    }

                    @Override
                    protected void onNoNetWork() {
                        super.onNoNetWork();
                        postState(StateConstants.NET_WORK_STATE);
                    }

                    @Override
                    public void onSuccess(NewsContentDTO newsContentDTO) {
                        Logger.e(TAG, newsContentDTO + "");
                        postData(EVENT_KEY_NEW_CONTENT_DATA, newsContentDTO);
                        postState(StateConstants.SUCCESS_STATE);
                    }

                    @Override
                    public void onFailure(String msg, int code) {
                        postState(code == Constants.TO_LOGIN_CODE ? StateConstants.NO_LOGIN_STATE : StateConstants.ERROR_STATE);
                    }

                }));
    }
}
